//
//  MainViewController.m
//  SlideZoomMenuDemo
//
//  Created by renxiaojian on 15/4/8.
//  Copyright (c) 2015年 renxiaojian. All rights reserved.
//

#import "MainViewController.h"
#import "MainCell.h"
#import "TFHpple.h"
#import "ChildCommonSense.h"
#import "NextViewController.h"
#import "UIImageView+WebCache.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>



@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"一周推荐";
    
    NSURL *url=[NSURL URLWithString:@"http://www.9baobao.com"];
    NSData *data=[NSData dataWithContentsOfURL:url];
    TFHpple *hpple=[[TFHpple alloc]initWithHTMLData:data encoding:@"NSUTF8StringEncoding"];
    NSArray *tmpArr=[hpple searchWithXPathQuery:@"//li [@class='image']/a/img"];
    
    NSMutableArray *tmpTitles=[[NSMutableArray alloc]init];
    NSMutableArray *tmpPic=[[NSMutableArray alloc]init];
    for (int i=0; i<tmpArr.count; i++) {
        TFHppleElement *s=tmpArr[i];
        [tmpTitles addObject:s.attributes[@"alt"]];
        [tmpPic addObject:s.attributes[@"src"]];
    }
    self.list=tmpTitles;
    self.imgList=tmpPic;
    
    NSLog(@"%@",self.imgList);
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ChildCommonSense" bundle:nil] forCellReuseIdentifier:@"ChildCommonSense"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.imgList.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChildCommonSense *cell = [tableView dequeueReusableCellWithIdentifier:@"ChildCommonSense"];
    UIImageView *imgView=(UIImageView *)[cell viewWithTag:100];
    NSString *adress=@"http://www.9baobao.com";
    
    NSString *urlPic=[self.imgList[indexPath.row] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *pic=[adress stringByAppendingString:urlPic];
    NSURL *url=[NSURL URLWithString:pic];
    SDWebImageOptions options=SDWebImageRetryFailed|SDWebImageLowPriority;
    [imgView sd_setImageWithURL:url placeholderImage:nil options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"下载进度:%f",(double)receivedSize/expectedSize);
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        NSLog(@"图片加载完毕:%@",image);
    }];
    cell.content.text = self.list[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NextViewController *nextVC = [NextViewController new];
    nextVC.title=self.list[indexPath.row];
    NSArray *arrWord=@[@"word1",@"word2",@"word3",@"word4"];
    nextVC.word=arrWord[indexPath.row];
    [self.navigationController pushViewController:nextVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
